<?php

if (!defined('SYSPATH')) exit("No direct script access allowed!");

function get_layout_url($layout) {
    return BASEURL . APPFOLDER . 'layout/' . $layout;
}

function get_script_url($script) {
    return BASEURL . APPFOLDER . 'js/' . $script;
}

function strtouppertr($metin) {

    if (LOCALE == 'tr_TR') {
        return mb_convert_case(str_replace('i','İ',$metin), MB_CASE_UPPER, "UTF-8");
    }
    else {
        return strtoupper($metin);
    }
}

function get_keys_exist() {

    if (func_num_args() == 0) {
        return TRUE;
    }

    for ($i = 0; $i < func_num_args(); $i++) {
        if (!key_exists(func_get_arg($i), $_GET)) {
            return FALSE;
        }
    }

    return TRUE;
}

function is_on_page($path) {

    $uri = CL_URI::get_instance();

    if (strpos(trim($uri->string, '/'), trim($path, '/')) === 0) {
        return true;
    }

    return false;
}

function json_encode_array($array) {
    return json_encode(json_convert_array($array));
}

function json_convert_array($array) {
    $jsonArray = array();
    foreach ($array as $i => $object) {
        array_push($jsonArray, $object->toJsonArray());
    }
    return $jsonArray;
}

function throw_json_exception($errstr = '', $errno = 0) {

    $exception = array(
        'errno' => $errno,
        'errstr' => $errstr
    );

    ob_clean();
    ob_start();

    header('Content-type: application/json');
    echo json_encode($exception);
    exit();
}

function print_json_success($message) {
    echo json_encode(array('success' => array('message' => $message)));
}

/**
 * Loads config variable from given file and returns it's reference.
 *
 * @staticvar array $configs
 * @param string $file
 * @return array
 */
function &get_config($file = 'main') {

    static $configs = array();

    if (isset($configs[$file])) {
        return $configs[$file];
    }

    if (!file_exists(APPPATH . '/config/' . $file . '.php')) {
        show_error("The configuration file $file.php does not exits.", "Config Error");
    }

    require_once APPPATH . '/config/' . $file . '.php';

    if (!isset($config) OR !is_array($config)) {
        show_error("The configuration file $file.php does not appear to be formatted correctly.", "Config Error");
    }

    $configs[$file] =& $config;
    return $configs[$file];
}

function controller_exists($class) {

    if (!file_exists(APPPATH . 'controllers/' . $class . '.php')) {
        return false;
    }

    return true;
}

function controller_is_callable($class, $method) {

    if (!method_exists($class, $method)) {
        return false;
    }

    return true;
}

function get_base_url() {
    return BASEURL;
}

function get_full_url($path = '') {
    $url = BASEURL . $path;
    return $url;
}

function get_app_url($path = '') {
    return BASEURL . 'app/' . APPNAME . '/' . $path;
}

function get_image_url($image) {
    return BASEURL . 'application/images/' . $image;
}

function get_limage_url($image, $locale = '') {

    if ($locale == '') {
        $lcl = CL_Locale::get_instance();
        $locale = $lcl->get_locale();
    }

    return BASEURL . 'application/pages/' . strtolower($locale) . '/images/' . $image;
}

function get_cimage_url($image) {
    return BASEURL . 'application/pages/common/images/' . $image;
}

function show_error($message, $header, $type = 'default') {

    $err = CL_Error::get_instance();

    if (key_exists('ajax', $_GET)) {
        echo $err->show_ajax_error($message, $type);
    }
    else {
        echo $err->show_error($message, $header, $type);
    }
    
    log_message($message);
    exit(-1);
}

function log_message($message) {
    $log = CL_Log::get_instance();
    $log->write($message);
}

function error_handler($errno, $errstr, $errfile, $errline) {
    show_error("$errstr in $errfile on line $errline.", "PHP Error");
}

function dfs($objects, $start_handler, $end_handler, $context = NULL, &$carrier = NULL) {

    $objectBuffer = array_reverse($objects);

    while (count($objectBuffer) > 0) {
        $object = array_pop($objectBuffer);

        if ($object === NULL) {
            if ($context !== NULL) {
                call_user_func_array(array($context, $end_handler), array($object, &$carrier));
            }
            else {
                call_user_func_array($end_handler, array($object, &$carrier));
            }
            continue;
        }

        if ($context !== NULL) {
            call_user_func_array(array($context, $start_handler), array($object, &$carrier));
        }
        else {
            call_user_func_array($start_handler, array($object, &$carrier));
        }
        array_push($objectBuffer, NULL);

        if ($object->has_children()) {
            $objectBuffer = array_merge($objectBuffer, array_reverse($object->get_children()));
        }
    }
}

function get_localized_url($locale) {

    $uri = CL_URI::get_instance();
    $tail = '/';

    foreach ($uri->segments as $segment) {
        $tail .= $segment . '/';
    }

    return BASEURL . $locale . $tail;
}


/* End of file Common.php */
/* Location: /system/codelite/Common.php */